{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "024deba1",
   "metadata": {},
   "source": [
    "# 第三周学习笔记"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea1c6021",
   "metadata": {},
   "source": [
    "## 注释\n",
    "- 注释（Comments）用来向用户提示或解释某些代码的作用和功能，它可以出现在代码中的任何位置。Python 解释器在执行代码时会忽略注释，不做任何处理，就好像它不存在一样。\n",
    "- 在调试（Debug）程序的过程中，注释还可以用来临时移除无用的代码。\n",
    "- 一般情况下，合理的代码注释应该占源代码的 1/3 左右。\n",
    "- Python 支持两种类型的注释，分别是单行注释和多行注释。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b1833a2b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, Python!\n"
     ]
    }
   ],
   "source": [
    "# 第一个注释\n",
    "# 第二个注释\n",
    " \n",
    "'''\n",
    "第三注释\n",
    "第四注释\n",
    "'''\n",
    " \n",
    "\"\"\"\n",
    "第五注释\n",
    "第六注释\n",
    "\"\"\"\n",
    "print (\"Hello, Python!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c130864d",
   "metadata": {},
   "source": [
    "## str.format()函数\n",
    "- [format方法](https://docs.python.org/3/tutorial/inputoutput.html#the-string-format-method)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "99d8175f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我叫蔡洪嘉，今年20岁。\n",
      "我爱学Python和C。\n",
      "身高178，家住深圳。\n",
      "我爱python。\n",
      "爱学代码。\n",
      "python好用。\n",
      "我爱学C、Java和Python。\n",
      "我是蔡洪嘉，今年20岁。\n",
      "我是蔡洪嘉，今年20岁。\n",
      "我是蔡洪嘉，年龄20岁。\n",
      "我是CaiHongjia，年龄20岁。\n",
      "我是蔡洪嘉，我学Python。\n",
      "我是蔡洪嘉, 我怕C。\n",
      "我是CaiHongjia, 我怕Python。\n",
      "我是蔡洪嘉，我学Python。我是CaiHongjia，我用python编程。\n"
     ]
    }
   ],
   "source": [
    "# 省略字段名传递位置参数\n",
    "print('我叫{}，今年{}岁。'.format('蔡洪嘉', 20))\n",
    "\n",
    "# 花括号个数可以少于位置参数的个数\n",
    "print('我爱学{}和{}。'.format('Python', 'C', 'Java'))\n",
    "\n",
    "# 花括号个数多于位置参数的个数则会报错\n",
    "# print('我还吃{}和{}。'.format('西红柿'))\n",
    "# IndexError: tuple index out of range\n",
    "\n",
    "# 通过数字形式的简单字段名传递位置参数\n",
    "print('身高{0}，家住{1}。'.format(178, '深圳'))\n",
    "\n",
    "# 数字形式的简单字段名可以重复使用。\n",
    "print('我爱{0}。\\n爱学{1}。\\n{0}好用。'.format('python', '代码'))\n",
    "\n",
    "# 体会把所有位置参数整体当成元组来取值\n",
    "print('我爱学{1}、{3}和{0}。'.format(\n",
    "    'Python', 'C', 'C++', 'Java', 'Matlab'))\n",
    "\n",
    "# 使用变量名形式的简单字段名传递关键字参数\n",
    "print('我是{name}，今年{age}岁。'.format(name='蔡洪嘉', age=20))\n",
    "\n",
    "# 关键字参数的顺序可以随意调换\n",
    "print('我是{name}，今年{age}岁。'.format(age=20, name='蔡洪嘉'))\n",
    "\n",
    "# 使用元组传参\n",
    "infos = '蔡洪嘉', 20, 'CaiHongjia'\n",
    "print('我是{}，年龄{}岁。'.format(*infos))\n",
    "print('我是{2}，年龄{1}岁。'.format(*infos))\n",
    "\n",
    "# 使用字典传参\n",
    "venom = {'name': '蔡洪嘉', 'study': 'Python'}\n",
    "print('我是{name}，我学{study}。'.format(**venom))\n",
    "\n",
    "# 同时使用元组和字典传参\n",
    "hulk = '蔡洪嘉', 'Python'\n",
    "captain = {'name': 'CaiHongjia', 'weapon': 'C'}\n",
    "print('我是{}, 我怕{weapon}。'.format(*hulk, **captain))\n",
    "print('我是{name}, 我怕{1}。'.format(*hulk, **captain))\n",
    "\n",
    "# 同时使用位置参数、元组、关键字参数、字典传参\n",
    "# 注意：\n",
    "# 位置参数要在关键字参数前面\n",
    "# *元组要在**字典前面\n",
    "tup = 'CaiHongjia',\n",
    "dic = {'weapon': 'python编程'}\n",
    "text = '我是{1}，我学{study}。我是{0}，我用{weapon}。'\n",
    "text = text.format(\n",
    "    *tup, '蔡洪嘉', study='Python', **dic)\n",
    "print(text)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "718e170c",
   "metadata": {},
   "source": [
    "## If else语句\n",
    "- 在 Python 中，可以使用 if else 语句对条件进行判断，然后根据不同的结果执行不同的代码，这称为选择结构或者分支结构。\n",
    "- Python 中的 if else 语句可以细分为三种形式，分别是 if 语句、if else 语句和 if elif else 语句。\n",
    "- A.  \n",
    ">  if 表达式：    \n",
    ">     代码块   \n",
    "- B.  \n",
    ">  if 表达式：   \n",
    ">     代码块 1      \n",
    ">  else：     \n",
    ">     代码块 2  \n",
    "- C.    \n",
    ">   if 表达式 1：\n",
    ">     代码块 1  \n",
    ">  elif 表达式 2：\n",
    ">     代码块 2\n",
    ">  elif 表达式 3：\n",
    ">     代码块 3\n",
    ">  ...//其它elif语句\n",
    ">  else：\n",
    ">     代码块 n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "49f6f4d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你的年龄：12\n",
      "警告：你还未成年，不能使用该软件！\n",
      "未成年人应该好好学习，读个好大学，报效祖国。\n",
      "软件正在使用中...\n"
     ]
    }
   ],
   "source": [
    "# 案例一：成年人监测\n",
    "if 表达式：\n",
    "    代码块 1\n",
    "else：\n",
    "    代码块 2\n",
    "age = int( input(\"请输入你的年龄：\") )\n",
    "if age < 18 :\n",
    "    print(\"警告：你还未成年，不能使用该软件！\")\n",
    "    print(\"未成年人应该好好学习，读个好大学，报效祖国。\")\n",
    "else:\n",
    "    print(\"你已经成年，可以使用该软件。\")\n",
    "    print(\"时间宝贵，请不要在该软件上浪费太多时间。\")\n",
    "print(\"软件正在使用中...\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "61460385",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入身高（米）：1.78\n",
      "输入体重（千克）：68\n",
      "BMI指数为：21.461936624163616\n",
      "正常范围，注意保持\n"
     ]
    }
   ],
   "source": [
    "# 案例二：BMI指数\n",
    "height = float(input(\"输入身高（米）：\"))\n",
    "weight = float(input(\"输入体重（千克）：\"))\n",
    "bmi = weight / (height * height)  #计算BMI指数\n",
    "if bmi<18.5:\n",
    "    print(\"BMI指数为：\"+str(bmi))\n",
    "    print(\"体重过轻\")\n",
    "elif bmi>=18.5 and bmi<24.9:\n",
    "    print(\"BMI指数为：\"+str(bmi))\n",
    "    print(\"正常范围，注意保持\")\n",
    "elif bmi>=24.9 and bmi<29.9:\n",
    "    print(\"BMI指数为：\"+str(bmi))\n",
    "    print(\"体重过重\")\n",
    "else:\n",
    "    print(\"BMI指数为：\"+str(bmi))\n",
    "    print(\"肥胖\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb24926f",
   "metadata": {},
   "source": [
    "## For 循环语句\n",
    "- Python for 循环可以遍历任何可迭代对象，如一个列表或者一个字符串。\n",
    "- for循环的一般格式如下：\n",
    "\n",
    "> for variable in sequence:      \n",
    "> &emsp;        statements    \n",
    "> else:    \n",
    "> &emsp;        statements    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2ede1d85",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C\n",
      "C++\n",
      "Perl\n",
      "Python\n"
     ]
    }
   ],
   "source": [
    "# 案例一：\n",
    "languages = [\"C\", \"C++\", \"Perl\", \"Python\"] \n",
    "for x in languages:\n",
    "    print (x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "6ddd1fec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "# 如果需要遍历数字序列，可以使用内置range()函数。它会生成数列，例如:\n",
    "for i in range(5):\n",
    "     print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "daecb192",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "6\n",
      "7\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "# 也可以使用range指定区间的值：\n",
    "for i in range(5,9) :\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "5edc1cb7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "3\n",
      "6\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "# 也可以使range以指定数字开始并指定不同的增量(甚至可以是负数，有时这也叫做'步长'):\n",
    "for i in range(0, 10, 3) :\n",
    "    print(i)\n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "674c3ba2",
   "metadata": {},
   "source": [
    "## range()方法    \n",
    "\n",
    "### 函数语法\n",
    "- range(stop)\n",
    "- range(start, stop[, step])  \n",
    "\n",
    "### 参数说明：\n",
    "- start: 计数从 start 开始。默认是从 0 开始。例如range（5）等价于range（0， 5）;\n",
    "- stop: 计数到 stop 结束，但不包括 stop。例如：range（0， 5） 是[0, 1, 2, 3, 4]没有5；\n",
    "- step：步长，默认为1。例如：range（0， 5） 等价于 range(0, 5, 1)。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "7e10bf9a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(0, 5)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "range(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "8267c9f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range(5):\n",
    "     print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "48b94459",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d3dbeb7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "9b68df20",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 5, 10, 15, 20, 25]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(0, 30, 5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "9a9b114f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(0, 10, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "eae9bbf6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(0, -10, -1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "2705824b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(1, 0))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9961cd49",
   "metadata": {},
   "source": [
    "## 用户输入\n",
    "- 1.input方法；\n",
    "- 2.字符串拼接+打印；\n",
    "- 3.%s占位符的使用；\n",
    "- 4.format方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "b931ab71",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "username:caihongjia\n",
      "password:123\n",
      "caihongjia 123\n"
     ]
    }
   ],
   "source": [
    "username = input(\"username:\")\n",
    "password = input(\"password:\")\n",
    "print(username,password)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "116f76c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name:caihongjia\n",
      "age:20\n",
      "job:student\n",
      "salary:1000\n",
      "----- INFO OF caihongjia------\n",
      "age:20\n",
      "job:student\n",
      "salary:1000\n"
     ]
    }
   ],
   "source": [
    "name = input(\"name:\")\n",
    "age = input(\"age:\")\n",
    "job = input(\"job:\")\n",
    "salary = input(\"salary:\")\n",
    "\n",
    "info = '''----- INFO OF ''' + name +'''------''' + '''\n",
    "age:''' + age+'''\n",
    "job:''' + job +'''\n",
    "salary:'''+salary\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "800d87d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name:caihongjia\n",
      "age:20\n",
      "job:student\n",
      "salary:1000\n",
      "-------INFO OF caihongjia -------\n",
      "Name:caihongjia\n",
      "Age:20\n",
      "Job:student\n",
      "Salary:1000\n",
      "\n"
     ]
    }
   ],
   "source": [
    "name = input(\"name:\")\n",
    "age = input(\"age:\")\n",
    "job = input(\"job:\")\n",
    "salary = input(\"salary:\")\n",
    "\n",
    "info = '''-------INFO OF %s -------\n",
    "Name:%s\n",
    "Age:%s\n",
    "Job:%s\n",
    "Salary:%s\n",
    "'''% (name,name,age,job,salary)\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "d172eadb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------INFO OF caihongjia -------\n",
      "Name:caihongjia\n",
      "Age:20\n",
      "Job:student\n",
      "Salary:1000\n",
      "\n"
     ]
    }
   ],
   "source": [
    "info2 =  '''-------INFO OF {_name} -------\n",
    "Name:{_name}\n",
    "Age:{_age}\n",
    "Job:{_job}\n",
    "Salary:{_salary}\n",
    "'''.format(_name=name,\n",
    "           _age=age,\n",
    "           _job=job,\n",
    "           _salary=salary)\n",
    "print(info2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21aa6cdf",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
